;*****************************************************************************************************
;+
; NAME:
;
;      IGL_InitSysVars
;
; PURPOSE:
;
;      This routine initializes the global system
;      and private variables used internally by the Interactive Graphics Library.
;      If you want to use the IGL routines on a regular basis, it is advised to
;      add a call to this routine in the IDL startup file.
;
; CALLING SEQUENCE:
;
;      IGL_InitSysVars
;
; ARGUMENTS:
;
;      None.
;                 
; KEYWORD PARAMETERS:
;
;      None.
;
;
; SIDE EFFECTS:
;
;      The system variables !IGL_D, !IGL_P, !IGL_X, !IGL_Y and !IGL_Z
;      are defined. They are the equivalent of the !D, !P, !X, !Y and
;      !Z structures respectively.
;
; PACKAGE:
;
;    Interactive Graphics Library (IGL)
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
;
; MODIFICATION HISTORY:
;
;     Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
PRO IGL_InitSysVars

  COMPILE_OPT idl2, HIDDEN  
  ON_ERROR, 2

  COMMON __IGL_PrivateVars__, privateGlobalObj 

  ;; DEVICE structure
  defsysv, '!IGL_D', exists=alreadyExists
  IF (NOT alreadyExists) THEN BEGIN
     value = {!IGL_DEVICE, $
              x_size:640L, $
              y_size:480L, $
              window:-1L, $
              win_state:bytarr(50), $
              notoolbar:0B $
             }
     defsysv, '!IGL_D', value
  ENDIF

  ;; PLT structure
  defsysv, '!IGL_P', exists=alreadyExists
  IF (NOT alreadyExists) THEN BEGIN
     value = {!IGL_PLT, $
              axiscolor:'black', $
              background:'white', $
              charsize:0.0, $
              charthick:0.0, $
              color:'black', $
              font:-1L, $
              linestyle:0L, $
              multi:lonarr(5), $
              noclip:0L, $
              noerase:0L, $
              nsum:0L, $
              position:fltarr(4), $
              psym:0L, $
              region:fltarr(4), $
              subtitle:'', $
              symsize:0.0, $
              t:dblarr(4,4), $
              t3d:0L, $
              thick:0.0, $
              title:'', $
              ticklen:0.02 $
             }
     defsysv, '!IGL_P', value
  ENDIF

  ;; AXIS structure
  defsysv, '!IGL_X', exists=alreadyExists
  IF (NOT alreadyExists) THEN BEGIN
     value = {!IGL_AXIS,INHERITS !AXIS}
     struct_assign, !X, value
     defsysv, '!IGL_X', value
  ENDIF

  defsysv, '!IGL_Y', exists=alreadyExists
  IF (NOT alreadyExists) THEN BEGIN
     value = {!IGL_AXIS,INHERITS !AXIS}
     struct_assign, !Y, value
     defsysv, '!IGL_Y', value
  ENDIF

  defsysv, '!IGL_Z', exists=alreadyExists
  IF (NOT alreadyExists) THEN BEGIN
     value = {!IGL_AXIS,INHERITS !AXIS}
     struct_assign, !Z, value
     defsysv, '!IGL_Z', value
  ENDIF

  ;; Private global variables will be stored using the same
  ;; default mechanics as the catalyst library
  IF (NOT OBJ_VALID(privateGlobalObj)) THEN BEGIN

     ;; Store the global private variables in a CATLIST object
     privateGlobalObj = obj_new('CatList')

     ;; Keep track of the current window (We do not rely on !IGL_D.window
     ;; because it is NOT a read-only system variable since we want to
     ;; update it and the user might unintentionally change its value)
     privateGlobalObj -> Add, 'currentWindow', value=-1L, success=success, /destroy_on_cleanup
     IF (NOT success) THEN message, 'Failed to initialize IGL global system variables'
     
     ;; Keep track of object references to know which windows are open
     privateGlobalObj -> Add, 'WindowObjRefs', value=objarr(50), success=success, /destroy_on_cleanup
     IF (NOT success) THEN message, 'Failed to initialize IGL global system variables'

     ;; Keep track of current active coordinate system (CATCOORD
     ;; object)
     privateGlobalObj -> Add, 'currentCoord', value=obj_new(), success=success, /destroy_on_cleanup
     IF (NOT success) THEN message, 'Failed to initialize IGL global system variables'

     ;; Private color table to prevent interferences with IDL color table
     privateGlobalObj -> Add, 'colorTable', value=obj_new('IGL_ColorTable'), success=success, /destroy_on_cleanup
     
  ENDIF

END
